#include "WidgetVariantIntList.h"
#include "ui_WidgetVariantIntList.h"
#include <iostream>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QListWidget>

WidgetVariantIntList::WidgetVariantIntList(QWidget *parent) :
    WidgetVariant(parent),
    ui(new Ui::WidgetVariantIntList)
{
    ui->setupUi(this);
    QObject::connect(ui->buttonAdd, SIGNAL(clicked(bool)),
                     this, SLOT(slotButtonAdd(bool)));
    QObject::connect(ui->buttonRemove, SIGNAL(clicked(bool)),
                     this, SLOT(slotButtonRemove(bool)));
}

WidgetVariantIntList::~WidgetVariantIntList()
{
    ui->listWidget->clear();
    delete ui;
}

void WidgetVariantIntList::refreshByProperty( EItemMetaPropertyPtr p)
{
    /*
    WidgetVariant::refreshByProperty(property);
    ui->labelName->setText( property.nameCN.isEmpty() ? property.name : property.nameCN);
    QVariantList vl = property.value.toList();
    for (int i = 0; i < vl.size(); i++)
    {
        int value = vl[i].toInt();
        QListWidgetItem* newItem = new QListWidgetItem();
        QSpinBox* spin = new QSpinBox();
        spin->setValue(value);
        spin->setButtonSymbols(QAbstractSpinBox::NoButtons);
        ui->listWidget->addItem(newItem);
        ui->listWidget->setItemWidget(newItem, spin);
	    QObject::connect(spin, SIGNAL(valueChanged(int)), this, SLOT(slotSpinValueChanged(int)));
    }
    // ui->spinBox->setValue(property.value.toInt());
    */
}

void WidgetVariantIntList::updateDataAndEmitSignal()
{
    QList<QVariant> vl;
    for(int i =0; i < ui->listWidget->count(); i++)
    {
        QListWidgetItem* item = ui->listWidget->item(i);
        QSpinBox* spin = dynamic_cast<QSpinBox*>(ui->listWidget->itemWidget(item));
        int value = spin->value();
        vl.push_back(QVariant(value));
    }
    // emit signalVariantChanged(WidgetVariant::getModuleName(),  WidgetVariant::getProperty().name, vl);
}

void WidgetVariantIntList::slotSpinValueChanged(int value)
{
    // emit signalVariantChanged(WidgetVariant::getModuleName(),  WidgetVariant::getProperty().name, QVariant(value));
    // 更新o
    updateDataAndEmitSignal();
}

void WidgetVariantIntList::slotButtonAdd(bool boolValue)
{
    // std::cout<<"Debug WidgetVariantIntList::slotButtonAdd"<<std::endl;
    QListWidgetItem* newItem = new QListWidgetItem();
    QSpinBox* spin = new QSpinBox();
    spin->setButtonSymbols(QAbstractSpinBox::NoButtons);
    ui->listWidget->addItem(newItem);
    ui->listWidget->setItemWidget(newItem, spin);
    spin->setValue(ui->listWidget->count());
	QObject::connect(spin, SIGNAL(valueChanged(int)), this, SLOT(slotSpinValueChanged(int)));
    updateDataAndEmitSignal();
}

void WidgetVariantIntList::slotButtonRemove(bool boolValue)
{
    // std::cout<<"Debug WidgetVariantIntList::slotButtonRemove"<<std::endl;
    if(ui->listWidget->count() > 1)
    {
        QListWidgetItem* itemPtr = ui->listWidget->item(ui->listWidget->count() - 1);
        ui->listWidget->removeItemWidget(itemPtr);
        ui->listWidget->takeItem(ui->listWidget->count() - 1);
        delete itemPtr;
        //QSpinBox* spin = dynamic_cast<QSpinBox*>(ui->listWidget->itemWidget(itemPtr));
        //delete spin;
        updateDataAndEmitSignal();
    }
}
